import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';

import '../../bean/bean_news.dart';
import '../../utils/toast_utils.dart';
import '../news/news_item_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/7/23.
///
/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/9/25.
///
/// gongzhonghao biglead
/// https://study.163.com/instructor/1021406098.htm
/// https://blog.csdn.net/zl18603543572
/// https://www.toutiao.com/c/user/token/MS4wLjABAAAAYMrKikomuQJ4d-cPaeBqtAK2cQY697Pv9xIyyDhtwIM/

///lib/page/catalogue/catalogue_main_page.dart
///目录主页面
class CatalogueNotiaMainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _CatalogueNotiaMainPageState();
  }
}

class _CatalogueNotiaMainPageState extends State<CatalogueNotiaMainPage> {
  @override
  void initState() {
    super.initState();
    _requestList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("通知通告"),
      ),
      body: RefreshIndicator(
        onRefresh: () async {
          await _requestList();
          return true;
        },
        child: AnimatedSwitcher(
          //动画时间
          duration: Duration(milliseconds: 500),
          //动画曲线
          switchInCurve: Curves.easeInOutBack,
          //动画方式 这里使用中心缩放
          transitionBuilder: (child, animation) =>
              FadeTransition(
                opacity: animation,
                child: child,
              ),
          //动画切换显示的子Widget
          child: SizedBox.expand(child: Center(child: buildListView(),),),
        ),
      ),
    );
  }

  buildListView() {
    if (_loading) {
      return LoadingWidget();
    }
    if (_list.length == 0) {
      return NoDataWidget(
        clickCallBack: () {
          _loading = true;
          isClickRefresh = true;
          _requestList();
          setState(() {});
        },
      );
    }
    return NewsItemPage(
      list: _list,
      key: UniqueKey(),
    );
  }

  List<NewsBean> _list;
  bool isClickRefresh = false;
  CancelToken _cancelToken;
  bool _loading = true;

  Future<bool> _requestList() async {
    Map<String, dynamic> map = Map();
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.notifal,
      queryParameters: map,
      cancelTag: _cancelToken,
    );
    _loading = false;
    if (responseInfo.success) {
      _list = [];
      List list = responseInfo.data;
      list.forEach((element) {
        NewsBean newsBean = NewsBean.fromJson(element);
        _list.add(newsBean);
      });
    }
    if (!mounted) return true;
    if (isClickRefresh && (_list == null || _list.length == 0)) {
      isClickRefresh = false;
      ToastUtils.showToast("已刷新");
    }
    setState(() {});

    return Future.value(true);
  }
}
